package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.address.EditAddressActivity;
import com.stn.zjx.givingtreasure.activity.userbankcard.ModifyUserBankCardActivity;
import com.stn.zjx.givingtreasure.activity.userbankcard.UserBankCardManagerActivity;
import com.stn.zjx.givingtreasure.bean.BankCard;
import com.stn.zjx.givingtreasure.domain.BankCardDomain;
import com.stn.zjx.givingtreasure.domain.StatusDomain;
import com.stn.zjx.givingtreasure.domain.UserAddressDomain;
import com.stn.zjx.givingtreasure.event.BankCardEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.UserBankCardNetImpl;
import com.stn.zjx.givingtreasure.service.UserService;
import com.stn.zjx.givingtreasure.table.User;
import com.vondear.rxtools.RxActivityTool;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import me.shaohui.bottomdialog.BottomDialog;

public class BankCardItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<BankCard> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public UserBankCardManagerActivity context;

   public BankCardItemListAdapter(UserBankCardManagerActivity context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_bank_card, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
                   IndexHolder ih= (IndexHolder) viewHolder;
       BankCard   dataBean= dataBeanList.get(position);
              ih.bank_card_name.setText(String.valueOf(dataBean.getUserBankCardName()));
            ih.bank_card_type.setText(String.valueOf(dataBean.getUserBankCardType()));
            ih.bank_card_id.setText(String.valueOf(dataBean.getUserBankCardNo().substring(dataBean.getUserBankCardNo().length()-4,dataBean.getUserBankCardNo().length())));
              switch (dataBean.getUserBankCardStyle()){
                     case 0:
                       ih.bank_card_bg.setBackgroundResource(R.drawable.bank_card_style_blue);
                      break;
                  case 1:
                       ih.bank_card_bg.setBackgroundResource(R.drawable.bank_card_style_yellow);
                      break;
                  case 2:
                      ih.bank_card_bg.setBackgroundResource(R.drawable.bank_card_style_pink);
                      break;
              }
              ih.rootView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      showDialog(position,
                              dataBean.getUserBankCardId(),
                              dataBean.getUserRealName(),
                              dataBean.getUserPhone(),
                              dataBean.getUserIdCard(),
                              dataBean.getUserBankCardName(),
                              dataBean.getUserBankCardNo(),
                              dataBean.getUserBankCardType()

                      );
                  }
              });
   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<BankCard> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<BankCard> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void delete(int position){
        dataBeanList.remove(position);
        notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public void showDialog(int position,Integer userBankCardId,
                          String userRealName,
                          String userPhone,
                          String userIdCard,
                          String userBankCardName,
                          String userBankCardNo,
                          String userBankCardType){
       BottomDialog bankCardDialog= BottomDialog.create(context.getSupportFragmentManager())
               .setLayoutRes(R.layout.dialog_bank_card_modify)
               .setCancelOutside(false)
               .setDimAmount(0.4f)
               .setTag("bankCardDialog");
       bankCardDialog  .setViewListener(new BottomDialog.ViewListener() {
           @SuppressLint("SetTextI18n")
           @Override
           public void bindView(View v) {
                TextView title=v.findViewById(R.id.title);
               title.setText(context.getString(R.string.dialog_bank_card_modfiy_c1)+" "
               +userBankCardName+" "
               +context.getString(R.string.dialog_bank_card_modfiy_c2)
               + userBankCardNo.substring(userBankCardNo.length()-4,userBankCardNo.length())
               +context.getString(R.string.dialog_bank_card_modfiy_c3)
               +" "+userBankCardType+" "
               +context.getString(R.string.dialog_bank_card_modfiy_c4)
               );
               v.findViewById(R.id.cancel)
                       .setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View view) {
                               bankCardDialog.dismiss();
                           }
                       });
               v.findViewById(R.id.modify_bt)
                       .setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View view) {
                               bankCardDialog.dismiss();
                               Bundle bundle=new Bundle();
                               bundle.putInt("userBankCardId",userBankCardId);
                               bundle.putString("userRealName",userRealName);
                               bundle.putString("userIdCard",userIdCard);
                               bundle.putString("userBankCardName",userBankCardName);
                               bundle.putString("userBankCardNo",userBankCardNo);
                               RxActivityTool.skipActivity(context,ModifyUserBankCardActivity.class,bundle);

                           }
                       });
             v.findViewById(R.id.delete_bt)
             .setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     bankCardDialog.dismiss();
                     User onlineUser= UserService.getInstance(context).getOnlineLoginUser();
                     if(onlineUser==null){
                         return;
                     }
                     delete(position,onlineUser.getToken(),userBankCardId);
                 }
             });

           }
       });
       bankCardDialog.show();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
          public TextView bank_card_name;
          public  TextView bank_card_type;
          public TextView bank_card_id;
          public View bank_card_bg;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            bank_card_name=itemView.findViewById(R.id.bank_card_name);
            bank_card_type=itemView.findViewById(R.id.bank_card_type);
            bank_card_id=itemView.findViewById(R.id.bank_card_id);
            bank_card_bg=itemView.findViewById(R.id.bank_card_bg);
        }
   }

   public  void delete(int position,String token,Integer userBankCardId){
       UserBankCardNetImpl
               .delete_bank_card(token, userBankCardId, token + userBankCardId + "_delete_bank_card",
                       new StringCallback() {
                           @Override
                           public void onStart(Request<String, ? extends Request> request) {
                               super.onStart(request);
                             context.showLoading();
                           }

                           @Override
                           public void onFinish() {
                               super.onFinish();
                              context.dissmissLoading();
                           }

                           @Override
                           public void onSuccess(Response<String> response) {
                                if(response.isSuccessful()&&!context.isFinishing()){
                                    StatusDomain domain=new Gson().fromJson(response.body(),StatusDomain.class);
                                    switch (domain.getStatus()){
                                        case BaseApi.REQUEST_SUCCESS_CODE:
                                            delete(position);
                                            BankCardEvent event=new BankCardEvent();
                                            EventBus.getDefault().post(event);
                                            break;
                                         default:
                                             context.showToast(domain.getMessage());
                                             break;
                                    }
                                }


                           }
                       });
   }

}